//
//  SYMedicineDetailsViewController.m
//  songyao
//
//  Created by work on 16/10/20.
//  Copyright © 2016年 saikuo. All rights reserved.
//

#import "SYMedicineDetailsViewController.h"
#import "SYMedicineDetailsModel.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "SYMedicineIntroductionCell.h"
#import "SYMedicineDirectionCell.h"

@interface SYMedicineDetailsViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource> {
    SDCycleScrollView *_tableHeaderView;
    NSMutableDictionary *_headerViewDictionary;
    SYMedicineDetailsModel *_data;
}

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) SYMedicineDetailsModel *data;


@end

@implementation SYMedicineDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"药品详情";
    [self createNavigationBackItem];
    _headerViewDictionary = [NSMutableDictionary dictionary];
    self.view.backgroundColor = [UIColor grayBackgroundColor];
    [self requestData];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor grayBackgroundColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.view);
    }];
    _tableView.tableHeaderView = [self tableHeaderView];
    // Do any additional setup after loading the view.
}

- (SDCycleScrollView *)tableHeaderView {
    if (!_tableHeaderView) {
        NSArray *imagesURLStrings = @[
                                      @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                      @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                      @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                                      ];
        _tableHeaderView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2.0f) delegate:self placeholderImage:nil];
        _tableHeaderView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _tableHeaderView.pageDotColor = [UIColor whiteColor];
        _tableHeaderView.currentPageDotColor = [UIColor orangeNavControllerTintColor];
        _tableHeaderView.imageURLStringsGroup = imagesURLStrings;
        
    }
    return _tableHeaderView;
}

- (void)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)requestData {
    WS(weakSelf);
    [[SYHttpManager sharedHttpManager]requesGet:get_medicine_detail_url parameters:@{@"medicineId":_model.ID} success:^(id responseObject) {
        _data = [[SYMedicineDetailsModel alloc] initWithDictionary:(NSDictionary *)responseObject error:nil];
        if ([weakSelf.data.code integerValue] == 0) {
            if (weakSelf.data.data.medicinePicView.count) {
                __block NSMutableArray *array = [NSMutableArray array];
                [weakSelf.data.data.medicinePicView enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    SYMedicinePicModel *model = (SYMedicinePicModel *)obj;
                    if (model.pictureUrl) {
                        [array addObject:model.pictureUrl];
                    }
                }];
                _tableHeaderView.imageURLStringsGroup = array;
            }
            [_tableView reloadData];
        } else {
            
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)setModel:(SYMedicineModel *)model {
    _model = model;
}


#pragma mark - tableView - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            SYMedicineIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
            if (!cell) {
                cell = [[SYMedicineIntroductionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"c"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
            }
            cell.data = _data.data;
            return cell;
        }
            
            break;
        case 1: {
            SYMedicineDirectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"b"];
            if (!cell) {
                cell = [[SYMedicineDirectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"b"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.data = _data.data;
            return cell;
        }
        default:
            break;
    }
    return [UITableViewCell new];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - tableView - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            if (_data.data) {
                return 70 + ([SYMedicineIntroductionCell textSizeWithText:_data.data.introduction].height);

            }
        }
            break;
        case 1: {
            if (_data.data) {
                return 10 + [SYMedicineDirectionCell textSizeWithText:_data.data.direction].height;
            }
        }
        default:
            break;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    }
    return 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view;
    NSString *key = [NSString stringWithFormat:@"%ld",(long)section];
    if (_headerViewDictionary[key]) {
        view = (UIView *)_headerViewDictionary[key];
    } else {
        view = [self sectionHeader:section];
        [_headerViewDictionary setObject:view forKey:[NSString stringWithFormat:@"%ld",(long)section]];
    }
    return view;
}

- (UIView *)sectionHeader:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont titleFont];
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor grayTitleColor];
    switch (section) {
        case 0: {
            label.text = @"主题特卖";
            [label setFrame:CGRectMake(10, 5, SCREEN_WIDTH, 25)];
            [view addSubview:label];
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
            lineView.backgroundColor = [UIColor grayBackgroundColor];
            [view addSubview:lineView];
        }
            break;
        case 1: {
            label.text = @"说明书";
            [label setFrame:CGRectMake(10, 0, SCREEN_WIDTH, 25)];
            [view addSubview:label];
        }
            break;
        default:
            break;
    }
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
